<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="处方类别" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="全部"
          clearable
          size="small"
          style="width: 100px"
        >
          <el-option
            v-for="dict in dict.type.prescription_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="药品状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="全部"
          clearable
          size="small"
          style="width: 100px"
        >
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入药品名称/编码/生产厂家"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增药品</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >扫码录入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  :data="drugList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="药品编码" align="center" prop="number" />
      <el-table-column label="药品名称" align="center" prop="drugName" />
      <el-table-column label="规格" align="center" prop="specification" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.specification" :value="scope.row.specification"/>
        </template>
      </el-table-column>
      <el-table-column label="处方类别" align="center" prop="drugName" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.prescription_type" :value="scope.row.drugName"/>
        </template>
      </el-table-column>
      <el-table-column label="采购价" align="center" prop="spell" />
      <el-table-column label="售药价" align="center" prop="drugType" />
      <el-table-column label="生产厂家" align="center" prop="factory" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.factory" :value="scope.row.factory"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="drugForm" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.drug_status" :value="scope.row.drugForm"/>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" prop="validityInform" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.validityInform, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >复制</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >停用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listDrug, getDrug, delDrug, addDrug, updateDrug } from "@/api/yldrug/drug";

export default {
  name: "Information",
  dicts: ['sys_normal_disable','specification','drug_type','prescription_type','factory','drug_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 药品表格数据
      drugList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prescriptionType: null,
        number: null,
        barCode: null,
        drugName: null,
        spell: null,
        drugType: null,
        specification: null,
        drugForm: null,
        commonJudge: null,
        invoice: null,
        permitNumber: null,
        factory: null,
        stock: null,
        drugStatus: null,
        packUnit: null,
        baseCoefficient: null,
        doseCoefficient: null,
        purchasePrice: null,
        salePrice: null,
        separable: null,
        drugUsage: null,
        singleDose: null,
        frequency: null,
        drugDay: null,
        total: null,
        stockMax: null,
        stockMin: null,
        location: null,
        validityInform: null,
        description: null,
        adjustCount: null,
        revision: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询药品列表 */
    getList() {
      this.loading = true;
      listDrug(this.queryParams).then(response => {
        console.log(response)
        this.drugList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        prescriptionType: null,
        number: null,
        barCode: null,
        drugName: null,
        spell: null,
        drugType: null,
        specification: null,
        drugForm: null,
        commonJudge: null,
        invoice: null,
        permitNumber: null,
        factory: null,
        stock: null,
        drugStatus: 0,
        packUnit: null,
        baseCoefficient: null,
        doseCoefficient: null,
        purchasePrice: null,
        salePrice: null,
        separable: null,
        drugUsage: null,
        singleDose: null,
        frequency: null,
        drugDay: null,
        total: null,
        stockMax: null,
        stockMin: null,
        location: null,
        validityInform: null,
        description: null,
        adjustCount: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {

      this.$router.push({ path: "/yldrug/information/addInformation/0"   }).catch(()=>{});
      // this.reset();
      // this.open = true;
      // this.title = "添加药品";
    },
    /** 跳转到新增页面 */
    handleUpdate(row) {
      console.log(row.id )
      this.$router.push({ path: "/yldrug/information/addInformation/"+row.id  }).catch(()=>{});
      // this.reset();
      // const id = row.id || this.ids
      // getDrug(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改药品";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDrug(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrug(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除药品编号为"' + ids + '"的数据项？').then(function() {
        return delDrug(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/drug/export', {
        ...this.queryParams
      }, `drug_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
